package com.hongyun.tms.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.annotation.excel.ExcelExport;
import com.hongyun.tms.common.dto.prodconsume.ProdConsumeQueryDto;
import com.hongyun.tms.common.dto.prodconsume.ProdConsumeSaveReqDto;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.ProdConsumeVO;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.service.IProdConsumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 拖轮油电水统计 前端控制器
 * </p>
 *
 * @author liPeng
 * @since 2024-06-12
 */
@Api(tags = "拖轮油电水统计")
@RestController
@RequestMapping("/prodConsume")
public class ProdConsumeController {
    @Resource
    private IProdConsumeService consumeService;

    /**
     * 分页查询
     *
     * @return page
     */
    @GetMapping("list")
    public AjaxResult<PageResponseDto<ProdConsumeVO>> list(ProdConsumeQueryDto query) {
        PageResponseDto<ProdConsumeVO> result = consumeService.treeList(query);
        return AjaxResult.success(result);
    }

    @GetMapping("getById")
    @ApiOperation("单个查询")
    public AjaxResult<ProdConsumeVO> getList(@RequestParam("id") Long id) {
        ProdConsumeVO plan = consumeService.detail(id);
        return AjaxResult.success(MapperingUtils.copyProperties(plan, ProdConsumeVO.class));
    }

    @PostMapping("removeBatch")
    @ApiOperation("批量删除")
    public AjaxResult removeBatch(@RequestBody JSONObject jsonObject) {
        String ids = (String) jsonObject.get("ids");
        consumeService.removeByIds(Arrays.asList(ids.split(",")));
        return AjaxResult.success("删除成功");
    }

    @PostMapping("saveOrUpdate")
    @ApiOperation("更新或者新增")
    public AjaxResult saveOrUpdate(@RequestBody @Valid ProdConsumeSaveReqDto dto) {
        consumeService.saveAndUpdate(dto);
        return AjaxResult.success();
    }

    @GetMapping("exportData")
    @ExcelExport(value = ProdConsumeVO.class)
    public AjaxResult<List<ProdConsumeVO>> exportData(ProdConsumeQueryDto query, HttpServletResponse response) {
        List<ProdConsumeVO> exportList = consumeService.export(query);
        return AjaxResult.success(exportList);
    }
}
